package com.uyayo.framework.pagehelper;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 排序帮助类
 * uyayo-framework-pagehelper
 * Created by hfj on 2017/11/16.
 */
public class SortOrderHelper {
    /**
     * 排序sql拼接
     *
     * @param sortOrderMappings 排序字段和数据库列名映射集合
     * @param sort              排序字段，多个使用逗号隔开
     * @param order             排序方式，多个使用逗号隔开
     * @return String
     */
    public static String getOrderBy(List<SortOrderMapping> sortOrderMappings, String sort, String order) {
        StringBuilder stringBuilder = new StringBuilder("");

        if (sortOrderMappings == null || sortOrderMappings.size() < 1 || sort == null || order == null) {
            return stringBuilder.toString();
        }

        String[] sorts = sort.split(",");
        String[] orders = order.split(",");
        if (orders.length < 1 || sorts.length < 1 || sorts.length != orders.length) {
            return stringBuilder.toString();
        }

        for (int i = 0; i < sorts.length; i++) {
            for (SortOrderMapping sortOrderMapping : sortOrderMappings) {
                if (Objects.equals(sorts[i], sortOrderMapping.getProperty()) && StringUtils.isNotEmpty(orders[i])) {
                    if (i > 0) {
                        stringBuilder.append(" , ");
                    }
                    if (StringUtils.isNotEmpty(sortOrderMapping.getPrefix())) {
                        stringBuilder.append(sortOrderMapping.getPrefix());
                    }
                    stringBuilder.append(sortOrderMapping.getColumn()).append(" ").append(orders[i]);
                    break;
                }
            }
        }
        return stringBuilder.toString();
    }


    /**
     * 排序sql拼接
     *
     * @param sortOrderMappings 排序字段和数据库列名映射集合
     * @param sortOrders   排序字段
     * @return String
     */
    public static String getOrderBy(List<SortOrderMapping> sortOrderMappings, List<SortOrder> sortOrders) {
        StringBuilder stringBuilder = new StringBuilder("");

        if (sortOrderMappings == null || sortOrderMappings.size() < 1 || sortOrders == null || sortOrders.size() <=0) {
            return stringBuilder.toString();
        }

        int i = 0;
        for (SortOrder sortOrder: sortOrders) {
            for (SortOrderMapping sortOrderMapping : sortOrderMappings) {
                if (Objects.equals(sortOrder.getProperty(), sortOrderMapping.getProperty()) && StringUtils.isNotEmpty(sortOrder.getOrder())) {
                    if (i > 0) {
                        stringBuilder.append(" , ");
                    }
                    if (StringUtils.isNotEmpty(sortOrderMapping.getPrefix())) {
                        stringBuilder.append(sortOrderMapping.getPrefix());
                    }
                    stringBuilder.append(sortOrderMapping.getColumn()).append(" ").append(sortOrder.getOrder());
                    break;
                }
            }
            i++;
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        List<SortOrderMapping> sortOrderMappers = new ArrayList<>();
        String sort = "createdTime,modifiedTime";
        String order = "asc,desc";
        sortOrderMappers.add(new SortOrderMapping("createdTime", "created_time"));
        sortOrderMappers.add(new SortOrderMapping("modifiedTime", "modified_time"));
        sortOrderMappers.add(new SortOrderMapping("sortOrder", "sort_order"));
        String orderBy = SortOrderHelper.getOrderBy(sortOrderMappers, sort, order);

        System.out.println(orderBy);



        List<SortOrderMapping> sortOrderMappersS = new ArrayList<>();
        List<SortOrder> sortOrders = new ArrayList<>();
        SortOrder sortOrder = new SortOrder();
        sortOrder.setProperty("createdTime");
        sortOrder.setOrder("DESC");
        SortOrder sortOrder2 = new SortOrder();
        sortOrder2.setProperty("modifiedTime");
        sortOrder2.setOrder("ASC");
        sortOrders.add(sortOrder);
        sortOrders.add(sortOrder2);

        sortOrderMappersS.add(new SortOrderMapping("createdTime", "created_time"));
        sortOrderMappersS.add(new SortOrderMapping("modifiedTime", "modified_time"));
        sortOrderMappersS.add(new SortOrderMapping("sortOrder", "sort_order"));
        String orderByS = SortOrderHelper.getOrderBy(sortOrderMappersS, sortOrders);

        System.out.println(orderByS);


    }
}
